//
//  ZXTabBarController.swift
//  loveBee
//
//  Created by zhangxing on 2017/8/12.
//  Copyright © 2017年 zhangXing. All rights reserved.
//

import UIKit

class ZXTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white

        //设置子控制器(调用自定义的方法)
        addChildViewControllers()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //  MARK: --   设置子控制器
    private func addChildViewControllers() {
        setupChildViewController(viewController: ZXHomeCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout()), imageName: "v2_home", title: "首页")
        
        setupChildViewController(viewController: ZXSortViewController(), imageName: "category", title: "分类")
        
        setupChildViewController(viewController: ZXShopingCarViewController(), imageName: "shopCart", title: "购物车")
        
        setupChildViewController(viewController: ZXMineViewController(), imageName: "v2_my", title: "我的")
    }
    

    //  MARK: --   创建子控制器
    private func setupChildViewController(viewController: UIViewController, imageName: String, title: String) {
        
        //设置未选中图片(这里加了一个init就会有提示)
        viewController.tabBarItem.image = UIImage.init(named: imageName)?.withRenderingMode(.alwaysOriginal)
        
        //设置选中的图片
        viewController.tabBarItem.selectedImage = UIImage.init(named: imageName + "_r")?.withRenderingMode(.alwaysOriginal)
        
        //设置标题文字
        viewController.title = title
        
        //设置选中文字的颜色(首先给他一个[],因为这里是一个字典数组)(我们在设置颜色的时候使用了桥接文件)
        viewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.init(hex: 0x737373), NSFontAttributeName: UIFont.systemFont(ofSize: 15)], for: .selected)
        
        //修改文字的偏移量
        viewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3)
        
        //创建navigationController设置根控制器
        let nav = UINavigationController(rootViewController: viewController)
        
        //添加到tabBar上
        addChildViewController(nav)
        
        
        
    }
    
    
    //  MARK: --   重写tabbar的点击方法做缩放动画
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //输出tabBar所有的子控件
        //        print(tabBar.subviews)
        
        /*
         [<_UIBarBackground: 0x7f9c6de05a20; frame = (0 0; 414 49); userInteractionEnabled = NO; layer = <CALayer: 0x60000002f480>>, <UITabBarButton: 0x7f9c6dc18080; frame = (2 1; 100 48); opaque = NO; layer = <CALayer: 0x608000232c20>>, <UITabBarButton: 0x7f9c6df2ff50; frame = (106 1; 99 48); opaque = NO; layer = <CALayer: 0x6000000386a0>>, <UITabBarButton: 0x7f9c6df31360; frame = (209 1; 100 48); opaque = NO; layer = <CALayer: 0x608000232780>>, <UITabBarButton: 0x7f9c6dc1e8d0; frame = (313 1; 99 48); opaque = NO; layer = <CALayer: 0x608000234300>>]
         */

        
        //其中items是[UITabBarItem]类型的,他是一个数组index(of: <#T##UITabBarItem#>),其中item就是这种类型的
        let index = tabBar.items?.index(of: item)
        
        
        
        //创建可变数组存储tabbarButton
        var tabBarButtonList = [UIView]()
        
        //遍历tabBar的子控件
        for tabBarButton in tabBar.subviews {
            
            //如果这个子控件是UITabBarButton类型的话,保存到tabBarButtonList这个可变数组
            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                tabBarButtonList.append(tabBarButton)
                
            }
            
            
        }
        
        //获取对应的按钮控件,其中tabBarBtn是UITabBarButton类型的
        let tabBarBtn = tabBarButtonList[index!]
        //输出tabBarBtn所有的子控件
//        print(tabBarBtn.subviews)
        
        /*
         [<UITabBarSwappableImageView: 0x7f96d8c05e00; frame = (37.3333 5.33333; 25 25); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x60000002a740>>, 
         
         <UITabBarButtonLabel: 0x7f96d8c13280; frame = (39.6667 32; 20.6667 12); text = '首页'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x600000091120>>]
         */
        
        
       
        //1.    读取tabBarBtn子控件的个数
//        print(tabBarBtn.subviews.count)
        /*
         2
         */

        //2.1   读出tabBarBtn子控件的首个元素
//        print(tabBarBtn.subviews.first!)
        
        /*
         <UITabBarSwappableImageView: 0x7fa7e3720480; frame = (37.3333 5.33333; 25 25); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x600000427d60>>
         */
        
        //2.2   读出tabBarBtn子控件的末个元素
//        print(tabBarBtn.subviews.last!)
        
        /*
         <UITabBarButtonLabel: 0x7fc35c605660; frame = (39.6667 32; 20.6667 12); text = '首页'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800009fcc0>>
         */

        
        //3.1   获取tabBarBtn子控件的首个元素UITabBarSwappableImageView
        let imageView = tabBarBtn.subviews.first
        
        //获取tabBarBtn子控件的末个元素UITabBarButtonLabel
        let label = tabBarBtn.subviews.last
        
        //让imageView做动画
        UIView.animate(withDuration: 0.125, animations: {
            //先让这个imageView先放大1.2倍
             imageView?.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
            
            //先让这个imageView先缩小到0.5倍
            label?.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
            
        }) { (_) in
            
            UIView.animate(withDuration: 0.125, animations: {
                //再让这个imageView回到原来的大小
                imageView?.transform = CGAffineTransform.identity
                
                //再让这个label回到原来的大小
                 label?.transform = CGAffineTransform.identity
                
            }, completion: { (_) in
                
            })
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}
